package cn.wolfcode.transfer.service.impl;

import cn.wolfcode.transfer.domain.Exam;
import cn.wolfcode.transfer.domain.ExamQuestion;
import cn.wolfcode.transfer.domain.InsertExam;
import cn.wolfcode.transfer.domain.Question;
import cn.wolfcode.transfer.mapper.ExamMapper;
import cn.wolfcode.transfer.mapper.ExamQuestionMapper;
import cn.wolfcode.transfer.mapper.QuestionItemMapper;
import cn.wolfcode.transfer.mapper.QuestionMapper;
import cn.wolfcode.transfer.qo.QueryObject;
import cn.wolfcode.transfer.service.IExamService;
import cn.wolfcode.transfer.util.QuestionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class ExamServiceImpl implements IExamService {

    @Autowired
    private ExamMapper examMapper;
    @Autowired
    private ExamQuestionMapper examQuestionMapper;
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private QuestionItemMapper questionItemMapper;


    @Override
    public void save(Exam exam) {
        examMapper.insert(exam);
    }

    @Override
    public void delete(Long id) {
        examMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Exam exam) {
        examMapper.updateByPrimaryKey(exam);
    }

    @Override
    public Exam get(Long id) {
        return examMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Exam> listAll() {
        return examMapper.selectAll();
    }

    @Override
    public PageInfo<Exam> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize()); //对下一句sql进行自动分页
        List<Exam> exams = examMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Exam>(exams);
    }

    @Override
    public Exam insertExam(InsertExam insertExam) {
        //试卷名称
        String title = insertExam.getTitle();
        //考试时间
        Integer examMinute = insertExam.getExamMinute();
        Exam exam = new Exam();
        exam.setExamMinute(examMinute);
        exam.setTitle(title);
        save(exam);
        //获取试卷Id
        Long id = exam.getId();
        //创建中间表对象
        ExamQuestion examQuestion = new ExamQuestion();
        //存储顺序数值
        int a = 1;
        //获取全部单选题
        System.out.println(QuestionUtil.单选.getValue());
        List<Question> singleChoice = questionMapper.getByType(QuestionUtil.单选.getValue());
        //获取单选题数量
        int singleChoiceSize = singleChoice.size();
        //获取需要的单选题数量
        int size = insertExam.getSingleChoice();
        //根据输入随机题目数量生成
        Random random = new Random();
        //生成随机单选题
        do {
            //随机题目
            Question question = (Question) singleChoice.get(random.nextInt(singleChoiceSize));
            if (examQuestion.getSingleChoice()==null||!examQuestion.getSingleChoice().contains(question)){
                examQuestion.getSingleChoice().add(question);
            }
        }while (examQuestion.getSingleChoice().size() != size);
            List<Question> multipleChoice = questionMapper.getByType(QuestionUtil.多选.getValue());
        //获取所有多选题数量
        int multipleChoiceSize = multipleChoice.size();
        //获取需要的多选题数量
        int size2 = insertExam.getMultipleChoice();
        //生成随机多选题
        do  {
            //随机题目
            Question question = (Question) multipleChoice.get(random.nextInt(multipleChoiceSize));
            if (examQuestion.getMultipleChoice()==null||!examQuestion.getMultipleChoice().contains(question)){
                examQuestion.getMultipleChoice().add(question);
            }
        }while ( examQuestion.getMultipleChoice().size() != size2);
        List<Question> judgeChoice = questionMapper.getByType(QuestionUtil.判断.getValue());
        //获取所有判断题数量
        int judgeChoiceSize = judgeChoice.size();
        //获取需要的判断题数量
        int size3 = insertExam.getJudgeChoice();
        //生成随机判断题
        do  {
            //随机题目
            Question question = (Question) judgeChoice.get(random.nextInt(judgeChoiceSize));
            if (examQuestion.getJudgeChoice()==null||!examQuestion.getJudgeChoice().contains(question)){
                examQuestion.getJudgeChoice().add(question);
            }

        }while (examQuestion.getJudgeChoice().size() != size2);
        //存单选
        for (Question question : examQuestion.getSingleChoice()) {
            examQuestionMapper.insert(id,question.getId(),a++,insertExam.getSingleScore());
        }
        //存多选
        for (Question question : examQuestion.getMultipleChoice()) {
            examQuestionMapper.insert(id,question.getId(),a++,insertExam.getMultipleScore());
        }
        //存判断
        for (Question question : examQuestion.getJudgeChoice()) {
            examQuestionMapper.insert(id,question.getId(),a++,insertExam.getJudgeScore());
        }

return exam;
    }
}
